//添加常用函数
(function(){
	yanhx.addFunc("datePicker",function(){
		yanhx.exec("date.open",this);
	});
	yanhx.addFunc("closeWin",function(){
		yanhx.exec("myWin.closeWin");
	});
	yanhx.addFunc("$detail",function(key){
		return "<a href='javascript:void(0)' callFunc='click:showDetail'>"+this[key]+"</a>";
	});
	//填充查询条件
	yanhx.addFunc("fillQryCond",function(param){
		var retBean={};
		var len=param.elem.length;
		for(var i=0;i<len;i++){
			retBean[param.elem.get(i).getAttribute("key")]=	param.elem.get(i).value;
		}
		return retBean;
	});
	yanhx.addFunc("_list_tree_change",function(){
		//处理树 checkbox 或者radio 变更事件 触发下级查询 
	});
	yanhx.addFunc("tree_hasSub",function(data){
		if(data.hasSub&&data.hasSub=="y") return true;
		else return false;
	});
	yanhx.addFunc("list_tree_qry_cond",function(elem,inst){
		this.conditions.top=inst.getData(elem)[this.schema.parentIdKey];
	});
	yanhx.addFunc("_openTabFromMenu",function(){
		yanhx.exec("noBubble");
		var data=treeHandle.getTreeData(this);
		var elem=this;
		$.postJSON("/do?action=menu.getSrc",{module:data.module},function(retBean){
			if(retBean.result==800){
				elem.children(".tree_ctrl").trigger("click");
			}
			else{
				top.yanhx.exec("tabHandle.open",retBean);
			}
		});
	});
	yanhx.addFunc("_tree_open",function(){
		this.curRow.attr("callFunc","click:_openTabFromMenu");
	});
	yanhx.addFunc("stopEvent",function(e){
		e.stopPropagation();
	});
	yanhx.addFunc("prevent",function(e){
		e.preventDefault();
	});
	yanhx.addFunc("noBubble",function(){
		yanhx.find("setBubble")(true);
	});
	yanhx.addFunc("drag",function(e){
		yanhx.exec("dragHandle.dragStart",this.parent());
	});
	yanhx.addFunc("listTable_callback",function(param){
		return tableHandle.exec(param);
	});
	yanhx.addFunc("saveForm",function(){
		var src=$src();
		var formItem=src.parents("div.formItem");
		var action=src.attr("do");
		//获取配置的 callback
		var back=src.attr("back");
		if(formItem.length){
			var bean=yanhx.exec("fillHandle.submit",{
				elem:formItem	
			});
			if(bean){
				$.postJSON("/do?action="+action,bean,function(data){
						yanhx.exec("myWin.alert",{
							content:'操作成功',
							func:function(){
								//配置back 则处理back事件
								if(back){
									yanhx.exec("formHandle.callback",{
										func:back,
										data:data
									});
								}
								//默认走弹出窗口模式
								else{
									var param=yanhx.exec("myWin.getParam");
									if(param&&param.callback){
										param.backParam.data=data;
										param.callback(param.backParam);
										yanhx.exec("myWin.removeParam");
									}
								}
							}
						});
						yanhx.exec("myWin.closeWin");
				});
			}
		}
	});
	
	yanhx.addFunc("doWinCancel",function(){
		var back=this.attr("back");
		if(back){
			yanhx.exec("formHandle.callback",{
				func:back,
				data:null
			});
		}else{
			yanhx.exec("myWin.closeWin");	
		}
	});
	
	yanhx.addFunc("doAlertClose",function(){
		yanhx.exec("myWin.closeAlert",$src().attr("val"));	
	});
	yanhx.addComponent("formHandle",{
		callback:function(param){
			this[param.func](param.data);
		},
		closeTab:function(data){
			if(!data){
				top.yanhx.exec("tabHandle.closeCurTab");
			}
			else{
				top.yanhx.exec("tabHandle.closeAndCallBack",data);
			}
		}
		
	});
	yanhx.addComponent("preSaveMap",{
		addFunc:function(param){
			this[param.key]=param.val;
		},
		mutil:function(cfg){
			return yanhx.exec("mutilHandle.getBean",cfg);
		}
	});
	yanhx.addComponent("bindFuncMap",{
		
	});
	$.fn.fire=function(type){
		yanhx.find("fireEvent")($(this),type);
	}
	$.fn.fill=function(data){
		yanhx.exec("fillHandle.fill",{
			elem:$(this),
			data:data
		})
	}
	
	//get 收集数据 valid为校验配置
	$.fn.getBean=function(valid){
		var cfg=null;
		if(valid&&window[valid])
			cfg=window[valid];
		
		return yanhx.exec("fillHandle.submit",{
			elem:$(this),
			valid:cfg
		});
	}
	$.fn.$tableRow=function(){
		return tableHandle.exec({
			table:$(this),
			func:'getRowData',
			data:$src()
		});
	}
	$.fn.fillSelect=function(data,cfg){
		//默认取id ,name
		if(!cfg){
			cfg={val:'id',text:'name'};
		}
		var t=$(this);
		if(!t.length) return;
		var len=data.length;
		for(var i=0;i<len;i++){
			$("<option value="+data[i][cfg.val]+">"+data[i][cfg.text]+"</option>").appendTo(t);
		}
	}
	$.fn.page=function(total,cond,func){
		var t=$(this);
		var inst=t.data("pageInst");
		if(!inst){
			inst=yanhx.exec("pageHandle.init",{cond:cond,table:t,func:func});
			t.data("pageInst",inst);
		}else{
			inst.cond=cond;
		}
		if(total==0){
			inst.pageDiv.hide();
		}
		else{
			inst.pageDiv.show();
			inst.setTool(total);
		}
	}
	$.fn.parentUtil=function(f){
		var t=$(this);
		if(!t.parent().length)
			return;
		var r=t.parent(f);
		if(r.length)
			return r;
		else
			return t.parent().parentUtil(f);
	};
	$.java=function(param){
		var temp=null;
		if(param.param){
			temp={json:JSON.stringify(param.param)};
		}
		if(param.showload){
			yanhx.exec("myWin.showload");
		}
		return jQuery.post("/do?action="+param.action, temp, function(data){
				yanhx.exec("myWin.hideload");
				var ret=$.doAjaxRet(data);
				if(ret.direct){
					//已跳转页面 不处理
				}
				else if(!ret){
					if(param.error){
						param.error(data);
					}
					else{
						yanhx.exec("myWin.alert",{
							content:data.mes
						});
					}
				}
				else{
					(param.callback)(data);	
				}
		},"json");
	};
	//设置对象居中
	$.fn.setCenter=function(){
		var t=$(this);
		var win=$.screen();
		var left=(win.width-t.width())/2;
		var top=(win.height-t.height())/2;
		if(left>0) t.css("margin-left",left);
		if(top>0) t.css("margin-top",top);
	};
	
	$.screen=function(){
		return {
			width:window.screen.availWidth,
			height:$(document).height()
		};
	};
//	if(window.frameElement){
//		//一秒设置一次 frame高度
//		window.setInterval(setFrameHeight,1000);
//	}
//	function setFrameHeight(){
//		if(window.frameElement.parentElement.style.display!="none")
//			window.frameElement.style.height=$(document).height()+"px";
//	};
	$.fn.minHeight=function(){
		var t=$(this);
		t.css("min-height",$(document).height()-t.offset().top-10);
	};
	$.fn.warnPanel=function(){
		var t=$(this);
		var doc=$(document);
		t.css("top",doc.height()-t.height());
		t.css("left",doc.width()-t.width()-30);
		scrollHandle.add({elem:t,top:doc.height()-t.height()});
		$(window).bind("resize",function(e){
			t.css("left",document.documentElement.clientWidth-t.width()-30);
		});
	};
	$(window).bind("scroll",function(e){
		scrollHandle.scroll(e);
	});
	window.scrollHandle={
		elems:[],
		len:0,
		key:'first_top',
		scroll:function(e){
			var b=$(document.body);
			for(var i=0;i<this.len;i++){
				var t=this.elems[i];
				if(!t||!t.parent().length){
					this.elems[i]=null;
					t=null;
					continue;
				}
				t.css("top",Number(t.attr(this.key))+b.scrollTop());
			}
		},
		add:function(param){
			this.elems.push(param.elem);
			param.elem.attr(this.key,param.top);
			this.len++;
		}
	};
	$.fn.open=function(opt){
		var t=$(this);
		yanhx.exec("myWin.open",{
			elem:t,
			model:opt.model,
			onclose:opt.onclose,
			title:opt.title,
			src:opt.src,
			callback:opt.callback
		});
	};
	$.confirm=function(opt){
		yanhx.exec("myWin.confirm",{
			content:opt.content,
			y:opt.y,
			n:opt.n
		});
	};
	$.alert=function(mes,func){
		yanhx.exec("myWin.alert",{
			content:mes,
			func:func
		});
	};
	$.fn.fillTree=function(name,param){
		return treeHandle.exec($(this),name,param);
	};
	yanhx.addFunc("saveOpen",function(){
		var action=this.attr("action");
		var elem=this.parentUtil("div.window_content");
		var bean=elem.getBean(this.attr("valid"));
		if(bean==null){
			$.warn("请正确填写表单信息");
			return;
		};
		$.java({
			action:action,
			param:bean,
			callback:function(ret){
				$.warn(ret.mes);
				yanhx.exec("myWin.closeWin");
			}
		});
	});
	window.fileUpload=function(elem){
		var t=$(elem);
		var temp=t.parentUtil("li.normalFile");
		var target=temp.find("input.submitField");
		t.unbind();
		t.attr("onmouseover",null);
		temp.fileUpLoad({
			 bind:{
			 	chooseFile:t,
			 	fileDesc:temp.find(".file_input"),
			 	submit:temp.find(".file_submit"),
			 	filePath:target
			 },
			 fileType:target.attr("fileType"),
			 autoSubmit:false,
			 zIndex:9999000,
			 doSubmit:function(file,ext){
				return true; 
			 },
			 action:target.attr("action")
		});
		t.trigger("mouseover");
	}
})();